package com.zh.note.huawei.twohundred.寻找最大价值的矿堆;

import java.util.Scanner;

/**
 * https://dream.blog.csdn.net/article/details/130861674
 */
public class Main {
    static int[][] direcitons = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[][] matrix = new int[300][300];
        boolean[][] visited = new boolean[300][300];
        int index = 0;
        while (!sc.hasNext("exit")) {
            String s = sc.nextLine();
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                matrix[index][i] = Integer.parseInt(String.valueOf(ch));
            }
            index++;
        }
        int maxValue = 0;
        for (int i = 0; i < 300; i++) {
            for (int j = 0; j < 300; j++) {
                int value = dfs(i, j, matrix, visited);
                if (value > maxValue) {
                    maxValue = value;
                }
            }
        }
        System.out.println(maxValue);

    }

    private static int dfs(int i, int j, int[][] matrix, boolean[][] visited) {

        if (i < 0 || j < 0 || j >= 300 || i >= 300 || matrix[i][j] == 0) {
            return 0;
        }
        if (visited[i][j]) {
            return 0;
        }
        visited[i][j] = true;
        int value = matrix[i][j];

        for (int[] direciton : direcitons) {
            int newRow = direciton[0] + i;
            int newCol = direciton[1] + j;
            value += dfs(newRow, newCol, matrix, visited);

        }
        return value;


    }
}
